package aimis.repo;

import aimis.domain.PlantingRecords;
import aimis.domain.Region;
import aimis.domain.TobaccoCrops;
import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

@Mapper
public interface AreaMapper {
    @Select("select * from regions where id = #{id}")
    Region IdFindRegion(int id);
    @Select("select  * from tobacco_crops where id = #{id}")
    TobaccoCrops IdFindCrops(int id);
    @Select("select * from planting_records where id = #{id}")
    PlantingRecords IdFindPlanting(int id);
    @Select("select * from planting_records where region_id = #{region_id}")
    List<PlantingRecords> RegIdFind(int region_id);
    @Select("select * from planting_records where sowing_date = #{sowing_date}")
    List<PlantingRecords> SowingDateFind(Date sowing_date);

    @Insert("insert into regions (name,planted_area,unplanted_area) values (#{name},#{planted_area},#{unplanted_area})")
    void InsertAres(String name, String planted_area, String unplanted_area);

    @Update("update regions set name = #{name} ,planted_area = #{planted_area} ,unplanted_area = #{unplanted_area} where id = #{region_id};")
    void updateAres(String name, String planted_area, String unplanted_area, int region_id);

    @Insert("insert into tobacco_crops (name,growth_cycle,photo_url) values (#{name},#{growth_cycle},#{photo_url})")
    void InsertTobacco(String name, String growth_cycle, String photo_url);

    @Update("update tobacco_crops set name = #{name} , growth_cycle = #{growth_cycle} , photo_url = #{photo_url} where id = #{crop_id};")
    void updateTobacco(String name, String growth_cycle, String photo_url, int crop_id);

    @Insert("insert into planting_records (region_id,crop_id,planting_area,sowing_date,harvest_date,number) values (#{region_id},#{crop_id},#{planting_area},#{sowing_date},#{harvest_date},#{number});")
    void InsertPlanting(int region_id,int crop_id, float planting_area,Date sowing_date, Date harvest_date, int number);

    @Select("select max(id) from regions")
    int NewRid();
    @Select("select max(id) from tobacco_crops")
    int NewTobaccoId();




//
    @Select("SELECT pr.id AS id,pr.sowing_date,pr.harvest_date,pr.planting_area,pr.number,r.name AS RName,r.planted_area,r.unplanted_area,tc.name AS name,tc.growth_cycle,tc.photo_url FROM planting_records pr JOIN regions r ON pr.region_id = r.id JOIN tobacco_crops tc ON pr.crop_id = tc.id;")
    List<PlantingRecords> getAllSearch();
//


//
    @Select("SELECT * FROM planting_records WHERE id = #{id}")
    PlantingRecords findById(Long id);
    @Delete("DELETE FROM planting_records WHERE id = #{id}")
    void deleteById(Long id);
    @Delete("DELETE FROM regions WHERE id = #{id}")
    void deleterById(Long id);
    @Delete("DELETE FROM tobacco_crops WHERE id = #{id}")
    void deleteCById(Long id);
//
}
